package github.leavesczy.reactivehttpsamples.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;

import github.leavesczy.reactivehttpsamples.R;

public class BridgeEditItem extends LinearLayout {
    private  EditText editTextView;
    public BridgeEditItem(Context context) {
        this(context, null);
    }

    public BridgeEditItem(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BridgeEditItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        try (TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BridgeEditItem)) {
            String leftText = ta.getString(R.styleable.BridgeEditItem_beLeftText);
            int leftTextColor = ta.getColor(R.styleable.BridgeEditItem_beLeftTextColor, Color.BLACK);
            float leftTextSize = ta.getDimension(R.styleable.BridgeEditItem_beLeftTextSize, 14);

            String rightTextHint = ta.getString(R.styleable.BridgeEditItem_beRightTextHint);
            int rightTextColor = ta.getColor(R.styleable.BridgeEditItem_beRightTextColor, Color.BLACK);
            float rightTextSize = ta.getDimension(R.styleable.BridgeEditItem_beRightTextSize, 14);
            int inputType = ta.getInt(R.styleable.BridgeEditItem_beRightInputType, 0);

            int bgResId = ta.getResourceId(R.styleable.BridgeEditItem_beBackgroundSrcId, R.drawable.shape_bridge_edit_item_bg);


            setOrientation(LinearLayout.HORIZONTAL);
            setGravity(Gravity.CENTER_VERTICAL);

            setWeightSum(5);

            //添加左侧 Text
            TextView leftTextView=new TextView(context);
            leftTextView.setText(leftText);
            leftTextView.setTextColor(leftTextColor);
            leftTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,leftTextSize);
            LinearLayout.LayoutParams leftParams=new LayoutParams(0,LayoutParams.MATCH_PARENT);
            leftParams.weight=2;
            leftParams.leftMargin= SizeUtils.dp2px(10);
            leftTextView.setGravity(Gravity.CENTER_VERTICAL);
            addView(leftTextView,leftParams);

            //添加右侧的 EditText
            editTextView=new EditText(context);
            editTextView.setHint(rightTextHint);
            editTextView.setTextColor(rightTextColor);
            editTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,rightTextSize);
            setInputType(editTextView,inputType);
            editTextView.setLines(1);
            LinearLayout.LayoutParams rightParams=new LayoutParams(0,LayoutParams.MATCH_PARENT);
            rightParams.weight=3;
            rightParams.rightMargin= SizeUtils.dp2px(10);
            editTextView.setGravity(Gravity.CENTER_VERTICAL);
            addView(editTextView,rightParams);


            setBackgroundResource(bgResId);

        }
    }

    private void setInputType(EditText editText, int inputType) {
        if(inputType==0){
            editText.setInputType(InputType.TYPE_CLASS_TEXT);
        }else if(inputType==1){
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        }else if(inputType==2){
            editText.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
        }
    }

    public void setEditTextEnable(boolean enabled){
        editTextView.setEnabled(enabled);
    }

    public String getEditTextStr(){
        return editTextView.getText().toString();
    }

    public void setEditTextStr(String textStr){
        editTextView.setText(textStr);
    }

}
